package com.whfc.wxmp.api.mach.v1.controller;

import cn.hutool.core.util.StrUtil;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.hdc.dto.AppDeviceDTO;
import com.whfc.hdc.service.AppDeviceService;
import com.whfc.mach.dto.*;
import com.whfc.mach.entity.AppMach;
import com.whfc.mach.param.MachPageDataQueryParam;
import com.whfc.mach.param.MachQueryParam;
import com.whfc.mach.service.AppMachService;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachDataService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-17
 */
@RestController
@RequestMapping("/mp/api/v1/mach")
public class WxmpMachDataController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxmpMachDataService wxMachService;

    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService commonDeviceService;

    @DubboReference(interfaceClass = AppMachService.class, version = "1.0.0")
    private AppMachService appMachService;

    @PostMapping("/map/list")
    public Result mapList(@CurrentWxUser WxUser user,
                          @Validated @RequestBody MachQueryParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备地图页列表", RequestConetxtUtil.getParameters());
        MapMachDTO data = wxMachService.mapList(param);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/list")
    public Result list(@CurrentWxUser WxUser user,
                       @Validated @RequestBody MachPageDataQueryParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备列表(分页)", RequestConetxtUtil.getParameters());
        PageData<AppMachDTO> pageData = wxMachService.list(param);
        return ResultUtil.success(pageData);
    }

    @PostMapping(value = "/machList")
    public Result list(@CurrentWxUser WxUser user,
                       @Validated @RequestBody MachQueryParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备列表(不分页)", RequestConetxtUtil.getParameters());
        ListData<AppMachDTO> listData = wxMachService.list(param);
        return ResultUtil.success(listData);
    }

    @PostMapping("/static")
    public Result machNumstatic(@CurrentWxUser WxUser user,
                                @Validated @RequestBody MachQueryParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计设备数量", RequestConetxtUtil.getParameters());
        MachDataStatDTO data = wxMachService.machNumstatic(param);
        return ResultUtil.success(data);
    }

    @GetMapping("/workHour/day")
    public Result machWorkHourDay(@CurrentWxUser WxUser wxUser,
                                  @RequestParam(value = "machId") Integer machId,
                                  @RequestParam(value = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询设备每日工时", RequestConetxtUtil.getParameters());
        AppMachWorkHourDayDTO workHourDayDTO = wxMachService.getMachWorkHourDayData(machId, date);
        return ResultUtil.success(workHourDayDTO);
    }

    @GetMapping("single/radar/data")
    public Result singleRardarMap(@CurrentWxUser WxUser wxUser,
                                  @RequestParam(value = "machId") Integer machId,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "date", required = false) Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询单机雷达图", RequestConetxtUtil.getParameters());
        MachRadarMapDTO data = wxMachService.getSingleRardarMap(machId, date);
        return ResultUtil.success(data);
    }

    /**
     * 单机7天运行数据
     *
     * @param user
     * @param machId
     * @param date
     * @return
     */
    @GetMapping("/weekData")
    public Result machWeekData(@CurrentWxUser WxUser user,
                               @RequestParam("machId") Integer machId,
                               @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询单机7天运行数据", RequestConetxtUtil.getParameters());
        ListData<MachCurveMapDayDTO> listData = wxMachService.getMachWeekData(machId, date);
        return ResultUtil.success(listData);
    }

    @GetMapping("/getRemainOil")
    public Result getRemainOil(@CurrentWxUser WxUser user,
                               @RequestParam("machId") Integer machId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备当前油耗", RequestConetxtUtil.getParameters());
        Oil oil = wxMachService.getRemainOil(machId);
        return ResultUtil.success(oil);
    }

    @GetMapping("/gps/log")
    public Result gpsLog(@CurrentWxUser WxUser wxUser,
                         @RequestParam(value = "machId") Integer machId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备运动轨迹", RequestConetxtUtil.getParameters());
        AppMachGpsDTO appMachGpsDTO = wxMachService.gpsLog(machId, date);
        return ResultUtil.success(appMachGpsDTO);
    }

    @GetMapping("/gps/latest")
    public Result gpsLatest(@CurrentWxUser WxUser wxUser,
                            @RequestParam(value = "machId") Integer machId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备当前位置", RequestConetxtUtil.getParameters());
        Gps gps = wxMachService.gpsLatest(machId);
        return ResultUtil.success(gps);
    }

    @GetMapping("multi/radar/data")
    public Result multiRardarMap(@CurrentWxUser WxUser wxUser,
                                 @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询集群雷达图", RequestConetxtUtil.getParameters());
        MachRadarMapDTO data = wxMachService.getMultiRardarMap(deptId);
        return ResultUtil.success(data);
    }

    @GetMapping("multi/curve/data")
    public Result multiCurveMap(@CurrentWxUser WxUser wxUser,
                                @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询集群曲线图", RequestConetxtUtil.getParameters());
        MachCurveMapDTO data = wxMachService.getMultiCurveMap(deptId);
        return ResultUtil.success(data);
    }

    @GetMapping("/oil/data")
    public Result getDayOilData(@CurrentWxUser WxUser wxUser,
                                @RequestParam("machId") Integer machId,
                                @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询设备一天的油量数据", RequestConetxtUtil.getParameters());
        MachRunDataDTO data = wxMachService.getDayOilData(machId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/gps/log/page")
    public Result gpsLogPage(@CurrentWxUser WxUser wxUser,
                         @RequestParam(value = "machId") Integer machId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取设备运动轨迹", RequestConetxtUtil.getParameters());
        AppMach machById = appMachService.getMachById(machId);
        Assert.assertNotNull(machById);
        String sn = machById.getSn();
        if(StrUtil.isBlank(sn)){
            return ResultUtil.success(new PageData(new ArrayList(),pageNum,pageSize,1,0L));

        }
        AppDeviceDTO device = commonDeviceService.getDeviceBySn(sn);

        PageData<AppMachDataLogDTO> appMachGpsDTO = wxMachService.gpsLogPage(device.getId(), date,pageNum,pageSize);
        return ResultUtil.success(appMachGpsDTO);
    }

}
